<template>
  <el-dialog
    title="上传签名证书"
    :visible="visible"
    width="800px"
    :before-close="closeForm"
    :close-on-click-modal="false"
  >
    <el-form ref="signEdit" :model="sign" :rules="rules" label-width="120px">
      <el-form-item label="上传文件" prop="FileId">
        <fileUpBtn v-model="sign.FileId" file-key="EmpCertFile" :file-uri.sync="fileUri" />
      </el-form-item>
      <el-form-item label="证书加密方式" prop="SignType">
        <el-select v-model="sign.SignType" style="width: 240px;" placeholder="证书加密方式">
          <el-option label="【国标算法】2048位RSA" :value="1" />
          <el-option label="【国标算法】3072位RSA" :value="2" />
          <el-option label="【国密算法】SM2" :value="3" />
        </el-select>
      </el-form-item>
      <el-form-item label="密码" prop="Pwd">
        <el-input
          v-model="sign.Pwd"
          type="password"
          maxlength="16"
          placeholder="密码"
          show-password
        />
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button type="primary" @click="save">保存</el-button>
      <el-button @click="reset">重置</el-button>
    </div>
  </el-dialog>
</template>

<script>
import * as empSignApi from '@/api/emp/empSign'
import fileUpBtn from '@/components/fileUp/FileUpBtn.vue'
export default {
  components: {
    fileUpBtn
  },
  props: {
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      fileUri: null,
      sign: {
        FileId: null,
        Pwd: null,
        SignType: 1
      },
      rules: {
        FileId: [
          {
            required: true,
            message: '文件不能为空',
            trigger: 'blur'
          }
        ],
        SignType: [
          {
            required: true,
            message: '签名类型不能为空',
            trigger: 'blur'
          }
        ],
        Pwd: [
          {
            required: true,
            message: '密码不能为空！',
            trigger: 'blur'
          },
          {
            min: 6,
            max: 16,
            message: '密码长度需在6-16位之间！',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  computed: {
  },
  watch: {
    visible: {
      handler(val) {
        if (val) {
          this.reset()
        }
      },
      immediate: true
    }
  },
  mounted() {},
  methods: {
    save() {
      this.$refs['signEdit'].validate((valid) => {
        if (valid) {
          this.add()
        } else {
          return false
        }
      })
    },
    async add() {
      const data = {
        FileId: this.sign.FileId[0],
        CertPwd: this.sign.Pwd,
        SignType: this.sign.SignType
      }
      const id = await empSignApi.UpSign(data)
      this.$message({
        message: '添加成功!',
        type: 'success'
      })
      this.$emit('update:visible', false)
      this.$emit('close', true, id)
    },
    reset() {
      this.sign.CertPwd = null
      this.sign.FileId = null
      this.sign.SignType = 1
      this.fileUri = null
    },
    closeForm() {
      this.$emit('update:visible', false)
      this.$emit('close', false)
    }
  }
}
</script>
